﻿using Admin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Admin.Controllers
{
    public class RoleController : ApiController
    {
        [HttpGet]
        public IHttpActionResult user(string Name, int page = 1, int limit = 10)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var datalist = db.Roles.AsEnumerable();
                if (!string.IsNullOrEmpty(Name))
                {
                    datalist = datalist.Where(p => p.Name.Contains(Name));
                }
                var count = datalist.Count();
                var list = datalist.Select(p => new {
                    Id = p.Id,
                    Name = p.Name
                }).Skip((page - 1) * limit).Take(limit).ToList();
                return Json(new { count = count, data = list });
            }
        }
        [HttpPost]
        public IHttpActionResult addUsers([FromBody]Roles add)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Roles.FirstOrDefault(p => p.Name == add.Name);
                if (list == null)
                {
                    db.Roles.Add(add);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "添加成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "添加失败！" });
                    }
                }
                else
                {
                    return Json(new { code = -2, msg = "用户名不能重复！" });
                }

            }
        }
        [HttpPost]
        public IHttpActionResult Edit([FromBody]Roles add)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list2 = db.Roles.FirstOrDefault(p => p.Name == add.Name);
                if (list2 == null)
                {
                    var list = db.Roles.FirstOrDefault(p => p.Id == add.Id);
                    list.Name = add.Name;
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "修改成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "修改失败！" });
                    }
                }
                else
                {
                    return Json(new { code = -2, msg = "用户名不能重复！" });
                }

            }
        }
        [HttpPost]
        public IHttpActionResult Delete([FromBody]Roles user)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list1 = db.RoleModules.FirstOrDefault(p => p.RoleId == user.Id);
                if (list1 == null)
                {
                    var list = db.Roles.FirstOrDefault(p => p.Id == user.Id);
                    db.Roles.Remove(list);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "删除成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "删除失败！" });
                    }
                }
                else
                {
                    return Json(new { code = -2, msg = "该角色不能删除" });
                }

            }
        }
        [HttpPost]
        public IHttpActionResult GetRoleList([FromBody] RoleModules rolem)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var d = db.RoleModules.Where(p => p.RoleId == rolem.RoleId);
                var AuthorizeInfo = db.Modules.Where(p => true);
                var tempdata = from a in AuthorizeInfo
                               where a.ParentId == 0
                               select new
                               {
                                   Id = a.Id,
                                   Name = a.Name,
                                   ParentId = a.ParentId,
                                   Ops = a.Ops,
                                   Path = a.Path,
                                   Weight = a.Weight,
                                   children = from t in AuthorizeInfo
                                              where t.ParentId == a.Id
                                              select new
                                              {
                                                  Id = t.Id,
                                                  Name = t.Name,
                                                  ParentId = t.ParentId,
                                                  Ops = t.Ops,
                                                  Path = t.Path,
                                                  Weight = t.Weight
                                              }
                               };
                //通过rid获取选中的角色模块
                List<int> mouduleid = new List<int>();
                var rolesid = db.RoleModules.Where(x => x.RoleId == rolem.RoleId).ToList();
                var roleid = rolesid.Select(x => new
                {
                    mouduleid = x.ModuleId
                }).ToList();
                foreach (var item in roleid)
                {
                    mouduleid.Add(Convert.ToInt32(item.mouduleid));
                }
                return Json(new { data = tempdata.ToList(), data2 = mouduleid });
            }
        }

        //更新角色模块
        public IHttpActionResult editrolemoudule([FromBody] lists ro)
        {
            using (CRMEntities db = new CRMEntities())
            {
                if (ro.str == "" || ro.str == null)
                {
                    return Json(new { code = -1, msg = "请先选择模块" });
                }
                var userridlist = db.RoleModules.Where(p => p.RoleId == ro.id).ToList();
                List<int> mid = new List<int>();
                var splitid = ro.str.Split(',').ToList();
                foreach (var item in splitid)
                {
                    mid.Add(Convert.ToInt32(item));
                }
                foreach (var item in userridlist)
                {

                    db.RoleModules.Remove(item);
                }
                db.SaveChanges();
                foreach (var item in mid)
                {
                    RoleModules the = new RoleModules() { RoleId = ro.id, ModuleId = item };
                    db.RoleModules.Add(the);
                }
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败" });
                }
            }
        }
    }
}
